package com.gm.sfc.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.FrameLayout;

/**
 * SlideView
 * @author $Author:$
 * @version $Revision:$
 */
public class SlideView extends FrameLayout {
    private LayoutInflater mInflater;

    private ViewTreeObserver mObserver;

    private View mMenuView;

    private View mFrontView;

    private int mLimitMargin;

    private boolean mSlidable = true;

    private boolean mIsOpen = false;

    /**
     * Constructor
     * @param context
     */
    public SlideView(Context context) {
        super(context);
        initialize();
    }

    /**
     * Constructor
     * @param context
     * @param attrs
     */
    public SlideView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    /**
     * Constructor
     * @param context
     * @param attrs
     * @param defStyle
     */
    public SlideView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    private void initialize() {
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        setBackgroundColor(Color.BLACK);

        mObserver = this.getViewTreeObserver();
        mObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);

                mLimitMargin = getWidth() / 6;
                if (mMenuView != null) {
                    mMenuView.setLayoutParams(new LayoutParams(getWidth() - mLimitMargin, getHeight()));
                    addView(mMenuView, 0);
                }
                if (mFrontView != null) {
                    addView(mFrontView, 1);
                }
            }
        });
    }

    public void setMenuView(int layoutResID) {
        mMenuView = mInflater.inflate(layoutResID, null);
    }

    public void addMenuView(View menuView) {
        mMenuView = menuView;
    }

    public void addFrontView(View frontView) {
        mFrontView = frontView;
        // TODO バグがあるためコメントアウト
        // mFrontView.setOnTouchListener(new OnTouchListener() {
        // private float downX;
        //
        // private int downLeftMargin;
        //
        // @Override
        // public boolean onTouch(View v, MotionEvent event) {
        // if (mSlidable) {
        // final ViewGroup.MarginLayoutParams params =
        // (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        //
        // switch (event.getAction()) {
        // case MotionEvent.ACTION_DOWN:
        // downX = event.getRawX();
        //
        // downLeftMargin = params.leftMargin;
        // break;
        // case MotionEvent.ACTION_MOVE:
        // params.leftMargin = downLeftMargin + (int) (event.getRawX() - downX);
        //
        // // 左端以上はスライドを制限
        // if (params.leftMargin < 0) {
        // params.leftMargin = 0;
        // }
        //
        // if (params.leftMargin > getWidth() - mLimitMargin) {
        // params.leftMargin = getWidth() - mLimitMargin;
        // }
        // setFrontViewParams(params);
        // break;
        // case MotionEvent.ACTION_UP:
        // // 離した位置によってスライドする位置を調整する
        // if (params.leftMargin >= getWidth() / 2) {
        // params.leftMargin = getWidth() - mLimitMargin;
        // mIsOpen = false;
        // } else {
        // params.leftMargin = 0;
        // mIsOpen = true;
        // }
        // setFrontViewParams(params);
        // break;
        // }
        // return true;
        // }
        // return false;
        // }
        // });
    }

    public void open() {
        if (mSlidable) {
            mIsOpen = true;
            ViewGroup.MarginLayoutParams params =
                    (ViewGroup.MarginLayoutParams) mFrontView.getLayoutParams();

            params.leftMargin = getWidth() - mLimitMargin;
            setFrontViewParams(params);
        }
    }

    public void close() {
        if (mSlidable) {
            mIsOpen = false;
            ViewGroup.MarginLayoutParams params =
                    (ViewGroup.MarginLayoutParams) mFrontView.getLayoutParams();

            params.leftMargin = 0;
            setFrontViewParams(params);
        }
    }

    private void setFrontViewParams(ViewGroup.MarginLayoutParams params) {
        mFrontView.layout(
                params.leftMargin,
                params.topMargin,
                params.leftMargin + mFrontView.getWidth(),
                params.topMargin + mFrontView.getHeight());
    }

    public void setSlidable(boolean slidable) {
        mSlidable = slidable;
    }

    public boolean isOpen() {
        return mIsOpen;
    }
}